package com.example.wxminiprogram.service.GoodsServiceImpl;

import com.example.wxminiprogram.mapper.ClassMapper;
import com.example.wxminiprogram.mapper.GoodsMapper;
import com.example.wxminiprogram.mapper.HomeMapper;
import com.example.wxminiprogram.pojo.Goods;
import com.example.wxminiprogram.pojo.GoodsList;
import com.example.wxminiprogram.pojo.MyTool;
import com.example.wxminiprogram.service.ClassService;
import com.example.wxminiprogram.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Random;
import java.util.UUID;

@Slf4j
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    ClassMapper classMapper;
    @Autowired
    HomeMapper homeMapper;
    @Override
    public List<Goods> list(){
        return this.goodsMapper.getAll();
    }

    @Override
    public Goods query(Integer id){
        Goods goods = this.goodsMapper.getById(id);
        goods.setGoods_pics(this.goodsMapper.getSwiperImageById(id));
        return goods;
    }

    @Override
    public List<Goods> listToShop(Integer shop_id){
        List<Goods> goods = this.goodsMapper.getByShopId(shop_id);
        //为每一个goods的pics属性填充内容
        //为goods_class_name填充内容
        goods.forEach(e -> {
            e.setGoods_pics(this.goodsMapper.getSwiperImageById(e.getId()));
            e.setGoods_class_name(this.classMapper.getL2NameById(e.getGoods_class()));
        });
        return goods;
    }
    @Override
    public Goods modifyGoods(Goods goods){
        this.goodsMapper.updateByGoods(goods);
        return this.goodsMapper.getById(goods.getId());
    }
    @Override
    public void insert(Goods goods){
        this.goodsMapper.insert(goods);
        String path = "src\\main\\resources\\static\\"+goods.getId().toString();
        File f = new File(path);
        if(!f.exists()){
            f.mkdir();
        }
        File swiperF = new File(path+"\\swiper_image");
        swiperF.mkdir();
        File introduceF = new File(path+"\\introduce_image");
        introduceF.mkdir();
        File iconF = new File(path+"\\icon_image");
        iconF.mkdir();
    }

    @Override
    public GoodsList search(Integer cid, String query, Integer pagenum, Integer pagesize){
        GoodsList goodsList = new GoodsList();
        goodsList.setPagenum(pagenum);
        pagenum = (pagenum-1)*pagesize;
        goodsList.setGoods(this.goodsMapper.search(cid,query,pagenum,pagesize));
        goodsList.setTotal(goodsList.getGoods().size());
        return goodsList;
    }
    @Override
    public void deleteGoodsById(Integer id){
        this.goodsMapper.deleteSwiperImageByGoods_id(id);
        this.homeMapper.deleteByGoodsId(id);
        File f = new File("src\\main\\resources\\static\\"+id.toString());
        if(f.exists()){
            MyTool.deleteDir(f);
        }
        this.goodsMapper.deleteGoodsById(id);
    }

    @Override
    public void uploadIcon(MultipartFile multipartFile, Integer id){
        String path = "src\\main\\resources\\static\\"+id.toString()+"\\icon_image\\icon."+multipartFile.getOriginalFilename().split("\\.")[1];
        File f = new File(path);

        if(!f.exists()){
            try {
                f.createNewFile();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        try (OutputStream outputStream = new FileOutputStream(f)) {
            outputStream.write(multipartFile.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        String goods_icon = "http://192.168.56.1:8080/"+id.toString()+"/icon_image/icon."+multipartFile.getOriginalFilename().split("\\.")[1];
        this.goodsMapper.updateIconById(goods_icon,id);
    }
    @Override
    public void uploadIntroduce(MultipartFile multipartFile,Integer id){
        String path = "src\\main\\resources\\static\\"+id.toString()+"\\introduce_image\\introduce."+multipartFile.getOriginalFilename().split("\\.")[1];
        File f = new File(path);
        if(!f.exists()){
            try {
                f.createNewFile();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        try (OutputStream outputStream = new FileOutputStream(f)) {
            outputStream.write(multipartFile.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        String goods_introduce = "http://192.168.56.1:8080/"+id.toString()+"/introduce_image/introduce."+multipartFile.getOriginalFilename().split("\\.")[1];
        this.goodsMapper.updateIntroduceById(goods_introduce,id);
    }
    @Override
    public void uploadSwiper(MultipartFile multipartFile,Integer id){
        Integer count = this.goodsMapper.getSwiperNumByGoodsId(id);
        while(this.goodsMapper.getSwiperNumByGoodsId(id)>=5){
            Integer minId = this.goodsMapper.getMinIdByGoodsId(id);
            String[] image = this.goodsMapper.getImageById(minId).split("/");
            String name = image[image.length-1];
            File df = new File("src\\main\\resources\\static\\"+id.toString()+"\\swiper_image\\"+name);
            df.delete();
            this.goodsMapper.deleteSwiperById(minId);
        }
        Integer maxId = this.goodsMapper.getMaxId();
        if(maxId == null){
            maxId = 0;
        }
        maxId+=1;
        String path = "src\\main\\resources\\static\\"+id.toString()+"\\swiper_image\\swiper"+ maxId.toString() +"."+multipartFile.getOriginalFilename().split("\\.")[1];
        File f = new File(path);
        if(!f.exists()){
            try {
                f.createNewFile();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        try (OutputStream outputStream = new FileOutputStream(f)) {
            outputStream.write(multipartFile.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        String goods_swiper = "http://192.168.56.1:8080/"+id.toString()+"/swiper_image/swiper"+ maxId.toString() +"."+multipartFile.getOriginalFilename().split("\\.")[1];
        this.goodsMapper.insertSwiperByGoodsId(goods_swiper,id);
    }

    @Override
    public void updateQualityById(Goods goods){
        goodsMapper.updateQualityById(goods);
    }

}
